import AsyncStorage from "@react-native-async-storage/async-storage";

class SorageManager {

    /**
     *
     * @param key
     * @param value
     * @returns {Promise<void>}
     */
    static set(key, value) {
        return AsyncStorage.setItem(key, value);
    }

    /**
     *
     * @param key
     * @returns {Promise<string | null>}
     */
     static get(key){
        return AsyncStorage.getItem(key).then(value => {
            if (value && value !== ''){
                const jsonVaule = JSON.parse(value)
                return jsonVaule
            }
        }).catch(()=>null)
    }

    /**
     *
     * @param {String}key
     * @param {mixed}newValue
     * @ret urns {Promise<void>}
     */
    static update(key,newValue){
        return AsyncStorage.getItem(key).then(oldValue=>{
            newValue = typeof  newValue === 'string' ? newValue : Object.assign({},oldValue,newValue)
            return AsyncStorage.setItem(key,JSON.stringify(newValue))
        })
    }

    /**
     *
     * @param key
     * @returns {Promise<void>}
     */
    static  delete(key){
        return AsyncStorage.removeItem(key)
    }

    /**
     *
     * @returns {(callback?: Callback) => Promise<void>}
     */
    static clear(){
        return AsyncStorage.clear
    }
}
export default SorageManager